home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / BG_SRC.ZIP / BG_CDPNT.C next >
Encoding:
C/C++ Source or Header  |  1993-01-09  |  11.5 KB  |  428 lines

  1. /*
  2.  *    B G _ C D P D N T . C  -- this is a library module of BG.C and is used
  3.  *                           -- along with some #defines to make the
  4.  *                           -- program portable, the C-Dependent part of
  5.  *                           -- the program, i.e. are we compiling for
  6.  *                           -- Microsoft-C, Mix-C, or Turbo-C.
  7.  *                           -- Mostly graphics routines in here. See COMP.H
  8.  *                           -- to see which compiler we are using.
  9.  * O F Ransen, 9th January 1993
  10.  */
  11.  
  12. #include "comp.h"
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <bios.h>
  16. #include <conio.h>
  17. #include <time.h>
  18. #include <signal.h>
  19. #include "bg.h"
  20.  
  21. /**************************************************************************/
  22.  
  23. void Go_Text_Mode ()
  24. /*
  25. PURPOSE: To go back to the original text mode
  26. */
  27. {
  28.     #if TURBO_C
  29.     closegraph () ;
  30.     #elif MS_C
  31.     _setvideomode (_DEFAULTMODE) ;
  32.     #elif MIX_C
  33.     setvideomode (DEFAULTMODE) ;
  34.     #endif
  35. }
  36.  
  37. /**************************************************************************/
  38.  
  39. #define EGA_YPIXELS  200
  40.  
  41. void Get_Display_Config (Disp_Cfg_t* Cfg)
  42. /*
  43. PURPOSE: To fill out the Cfg structure with data about the current display.
  44. */
  45. {
  46. #if MIX_C
  47.  
  48.     #define MIX_CHAR_WIDE      8   /* Pixel size of chars...     */
  49.     #define MIX_CHAR_HIGH      8   /* ...in graphics mode.       */
  50.     (void)getvconfig (&Mix_Cfg) ;
  51.     Cfg->X_Pixels  = Mix_Cfg.xpixels ;
  52.     Cfg->Y_Pixels  = Mix_Cfg.ypixels ;
  53.     Cfg->Aspect_H  = Mix_Cfg.aspect_h ;
  54.     Cfg->Aspect_V  = Mix_Cfg.aspect_v ;
  55.     Cfg->Char_Wide = MIX_CHAR_WIDE ;
  56.     Cfg->Char_High = MIX_CHAR_HIGH ;
  57.     Cfg->Text_Cols = Mix_Cfg.xpixels / Cfg->Char_Wide ;
  58.     Cfg->Text_Rows = Mix_Cfg.ypixels / Cfg->Char_High ;
  59.  
  60. #elif MS_C
  61.  
  62.     _getvideoconfig (&Ms_Cfg) ;
  63.     Cfg->X_Pixels  = Ms_Cfg.numxpixels ;
  64.     Cfg->Y_Pixels  = Ms_Cfg.numypixels ;
  65.  
  66.     if (Cfg->Y_Pixels == EGA_YPIXELS) {
  67.     Cfg->Aspect_H  = 20 ;
  68.     Cfg->Aspect_V  = 10 ;
  69.     } else /* VGA */ {
  70.     Cfg->Aspect_H  = 20 ;
  71.     Cfg->Aspect_V  = 20 ;
  72.     }
  73.     Cfg->Text_Cols = Ms_Cfg.numtextcols ;
  74.     Cfg->Text_Rows = Ms_Cfg.numtextrows ;
  75.     Cfg->Char_Wide = Ms_Cfg.numxpixels / Cfg->Text_Cols ;
  76.     Cfg->Char_High = Ms_Cfg.numypixels / Cfg->Text_Rows ;
  77.  
  78. #elif TURBO_C
  79.     Cfg->X_Pixels  = getmaxx() + 1 ;
  80.     Cfg->Y_Pixels  = getmaxy() + 1 ;
  81.     if (Cfg->Y_Pixels == EGA_YPIXELS) {
  82.     Cfg->Aspect_H  = 20 ;
  83.     Cfg->Aspect_V  = 10 ;
  84.     } else /* VGA */ {
  85.     Cfg->Aspect_H  = 20 ;
  86.     Cfg->Aspect_V  = 20 ;
  87.     }
  88.     Cfg->Char_Wide = textwidth ("A") ;
  89.     Cfg->Char_High = textheight("A") ;
  90.     Cfg->Text_Cols = Cfg->X_Pixels / Cfg->Char_Wide ;
  91.     Cfg->Text_Rows = Cfg->Y_Pixels / Cfg->Char_High ;
  92. #endif
  93. }
  94.  
  95. /**************************************************************************/
  96.  
  97. void Draw_Line (short x1, short y1, short x2, short y2, short Col)
  98. /*
  99. PURPOSE: To draw a line from (x0,y0)-(x1-y1) of Col.
  100. */
  101. {
  102.     #if MIX_C
  103.     (void)pen_color (Col) ;
  104.     move_to (x1,y1) ;
  105.     line_to (x2,y2) ;
  106.     #elif MS_C
  107.     _setcolor (Col) ;
  108.     _moveto (x1,y1) ;
  109.     _lineto (x2,y2) ;
  110.     #elif TURBO_C
  111.     setcolor (Col) ;
  112.     line (x1,y1,x2,y2) ;
  113.     #endif
  114. }
  115.  
  116. /**************************************************************************/
  117.  
  118. void Draw_Rect (short x0, short y0, short Wide, short High, short Col)
  119. /*
  120. PURPOSE: To draw an unfilled rectangle of Col.
  121. NOTES:   1) The horizontal lines of the rectangle should have exactly
  122.      Wide pixels, and the vertical lines exactly High pixels.
  123.      2) The extents of the rectangle are
  124.           (x0,y0) -- (x0+Wide-1,y0+High-1)
  125. */
  126. {
  127.     #if MIX_C
  128.     (void)pen_color (Col) ;
  129.     move_to (x0,y0) ;
  130.     box (Wide,High,FALSE) ;
  131.     #elif MS_C
  132.     _setcolor (Col) ;
  133.     _rectangle (_GBORDER,x0,y0,x0+Wide-1,y0+High-1) ;
  134.     #elif TURBO_C
  135.     setcolor (Col) ;
  136.     rectangle (x0,y0,x0+Wide-1,y0+High-1) ;
  137.     #endif
  138. }
  139.  
  140. /**************************************************************************/
  141.  
  142. void Fill_Rect (short x0, short y0, short Wide, short High, short Col)
  143. /*
  144. PURPOSE: To draw a filled rectangle of Col. See notes for Draw_Rect.
  145. */
  146. {
  147.     #if MIX_C
  148.     (void)pen_color (Col) ;
  149.     move_to (x0,y0) ;
  150.     box (Wide,High,TRUE) ;
  151.     #elif MS_C
  152.     _setcolor (Col) ;
  153.     _rectangle (_GFILLINTERIOR,x0,y0,x0+Wide-1,y0+High-1) ;
  154.     #elif TURBO_C
  155.     int Rect[8] ;
  156.     setcolor (Col) ;
  157.     setfillstyle (SOLID_FILL,Col) ;
  158.     Rect[0] = x0 ;        Rect[1] = y0 ;
  159.     Rect[2] = x0+Wide-1 ; Rect[3] = y0 ;
  160.     Rect[4] = x0+Wide-1 ; Rect[5] = y0+High ;
  161.     Rect[6] = x0        ; Rect[7] = y0+High ;
  162.     fillpoly (4,Rect) ;
  163.     #endif
  164. }
  165.  
  166. /**************************************************************************/
  167.  
  168. void Fill_Poly (short N_Points, short* Points, short Colour)
  169. /*
  170. PURPOSE: To do a solid poly fill.
  171. */
  172. {
  173.     setfillstyle (SOLID_FILL,Colour) ;
  174.     fillpoly (N_Points,(int*)Points) ;
  175. }
  176.  
  177. /**************************************************************************/
  178.  
  179. void Draw_Point (short x, short y, short c)
  180. /*
  181. PURPOSE: To draw a point on the screen.
  182. */
  183. {
  184.     #if MIX_C
  185.     writedot (y,x,c) ;
  186.     #elif MS_C
  187.     _setcolor (c) ;
  188.     _setpixel (x,y) ;
  189.     #elif TURBO_C
  190.     putpixel (x,y,c) ;
  191.     #endif
  192. }
  193.  
  194. /**************************************************************************/
  195.  
  196. #if MS_C
  197. static char Solid_Fill_Mask[8] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF} ;
  198. #endif
  199.  
  200. void Set_Solid_Fill_Style (void)
  201. /*
  202. PURPOSE: To set the pattern which will be used in filling to solid.
  203. */
  204. {
  205.     #if MS_C
  206.     _setfillmask (Solid_Fill_Mask) ;
  207.     #elif TURBO_C
  208.     setfillstyle (SOLID_FILL,0) ;
  209.     #endif
  210. }
  211.  
  212. /**************************************************************************/
  213.  
  214. void Draw_Ellipse (short X_Center, short Y_Center,
  215.            short X_Rad,    short Y_Rad,     short Col)
  216. /*
  217. PURPOSE: To draw an ellipse at a centre with x and y radii, of Col.
  218. */
  219. {
  220.     #if MIX_C
  221.     move_to (X_Center,Y_Center) ;
  222.     (void)pen_color (Col) ;
  223.     ellipse (X_Rad,Y_Rad,Col) ;
  224.     #elif MS_C
  225.     /* Check the following for actual extents of the ellipse... */
  226.     _setcolor (Col) ;
  227.     _ellipse (_GBORDER,X_Center-X_Rad,Y_Center-Y_Rad,
  228.                X_Center+X_Rad,Y_Center+Y_Rad) ;
  229.     #elif TURBO_C
  230.     setcolor (Col) ;
  231.     ellipse (X_Center,Y_Center,0,360,X_Rad,Y_Rad) ;
  232.     #endif
  233. }
  234.  
  235. /**************************************************************************/
  236.  
  237. void Fill_Ellipse (short X_Center, short Y_Center,
  238.            short X_Rad,    short Y_Rad,     short Col)
  239. /*
  240. PURPOSE: To draw a solid filled ellipse at a centre with x and y radii.
  241. */
  242. {
  243.     #define LIMIT 1
  244.     #if MIX_C
  245.     /* MIX_C does not fill well, so we draw concentric circles */
  246.     (void)pen_color (Col) ;
  247.     move_to (X_Center,Y_Center) ;
  248.     do {
  249.     ellipse (X_Rad,Y_Rad,Col) ;
  250.     if (X_Rad > LIMIT) {
  251.         X_Rad-- ;
  252.     }
  253.     if (Y_Rad > LIMIT) {
  254.         Y_Rad-- ;
  255.     }
  256.     } while ((X_Rad > LIMIT) || (Y_Rad > LIMIT)) ;
  257.     #elif MS_C
  258.     _setcolor (Col) ;
  259.     _ellipse (_GFILLINTERIOR,X_Center-X_Rad,Y_Center-Y_Rad,
  260.                  X_Center+X_Rad,Y_Center+Y_Rad) ;
  261.     do {
  262.     _ellipse (_GBORDER,X_Center-X_Rad,Y_Center-Y_Rad,
  263.                X_Center+X_Rad,Y_Center+Y_Rad) ;
  264.     if (X_Rad > LIMIT) {
  265.         X_Rad-- ;
  266.     }
  267.     if (Y_Rad > LIMIT) {
  268.         Y_Rad-- ;
  269.     }
  270.     } while ((X_Rad > LIMIT) || (Y_Rad > LIMIT)) ;
  271.     #elif TURBO_C
  272.     setcolor (Col) ; /* So that the outline is correct colour too */
  273.     setfillstyle (SOLID_FILL,Col) ;
  274.     fillellipse (X_Center,Y_Center,X_Rad,Y_Rad) ;
  275.     #endif
  276. }
  277.  
  278. /**************************************************************************/
  279.  
  280. void Graf_Text (short Row, short Col, char* String, short Color)
  281. /*
  282. PURPOSE: To output a string at Row, Col on the graphics screen.
  283. */
  284. {
  285.     extern Disp_Cfg_t Disp_Cfg ;
  286.  
  287.     #if MS_C
  288.     Row = Row + 1 ;
  289.     Col = Col + 1 ;
  290.     if ((Row == Disp_Cfg.Text_Rows) && (Col == Disp_Cfg.Text_Cols)) {
  291.     /* MSC does not allow you to put a char here, scrolls! */
  292.     Col-- ;
  293.     }
  294.     _settextcolor (Color) ;
  295.     _settextposition (Row,Col) ;
  296.     _outtext (String) ;
  297.  
  298.     #elif TURBO_C
  299.     Fill_Rect (Col*Disp_Cfg.Char_Wide,Row*Disp_Cfg.Char_High,
  300.            textwidth (String),textheight (String),BLACK) ;
  301.     setcolor (Color) ;
  302.     outtextxy (Col*Disp_Cfg.Char_Wide,Row*Disp_Cfg.Char_High,String) ;
  303.  
  304.     #endif
  305. }
  306.  
  307. /**************************************************************************/
  308.  
  309. void Text_At_Pixel (short x, short y, char* String, short Colour)
  310. /*
  311. PURPOSE: To draw pixel algined text of a certain colour.
  312. */
  313. {
  314.     setcolor (Colour) ;
  315.     outtextxy (x,y,String) ;
  316. }
  317. /**************************************************************************/
  318.  
  319. void Error_Exit (char* Last_Words)
  320. /*
  321. PURPOSE: To speak the Last_Words and expire.
  322. */
  323. {
  324.     extern FILE* Rec_File ;
  325.     extern boolean Debugging ;
  326.     extern char Globerr[GERR_LEN] ;
  327.  
  328.     printf ("\nERROR: %s",Last_Words) ;
  329.     printf ("\nGloberr = %s",Globerr) ;
  330.     printf ("\nHit a key") ; (void)getch () ;
  331.     closegraph () ;
  332.     printf ("\nERROR: %s",Last_Words) ;
  333.     printf ("\nGloberr = %s",Globerr) ;
  334.     if ((Rec_File != NULL) && (Debugging)) {
  335.     fprintf (Rec_File,"\nERROR: %s",Last_Words) ;
  336.     fprintf (Rec_File,"\nGloberr = %s",Globerr) ;
  337.     fclose (Rec_File) ;
  338.     }
  339.     exit (1) ; /* Exit from error condition */
  340. }
  341.  
  342. /**************************************************************************/
  343.  
  344. void Init_Rannum_Gen (void)
  345. /*
  346. PURPOSE: To initialise the random number generator so that the same
  347.      sequence will be used every time.
  348. */
  349. {
  350.     srand (1) ;
  351. }
  352.  
  353. /**************************************************************************/
  354.  
  355. void Randomise (void)
  356. /*
  357. PURPOSE: To initialise the random number generator so that a different
  358.      sequence will be used every time the program is run.
  359. */
  360. {
  361.     time_t t ;
  362.     (void)time (&t) ;
  363.     srand ((uint)t) ;
  364. }
  365.  
  366. /**************************************************************************/
  367.  
  368. int Int_Rand_Range (int least, int most)
  369. /*
  370. PURPOSE : To return a random integer in the range specified.
  371. NOTES   : 1) This has been carefully written to include least and most
  372.          in the return values
  373.       2) rand returns an integer in the range 0...32767 in Microsoft
  374.          C and 0..16383 in Power C. The Power C manual says it is
  375.          32767, but that appears to be wrong.
  376. */
  377. {
  378.     long delta,number,r ;
  379.  
  380.     delta  = (long)most - (long)least + (long)1 ;
  381.     r      = (long)rand () ;
  382.     number = (long)least + ((delta*r)/(long)RAND_MAX) ;
  383.  
  384.     if ((number < least) || (number > most)) {
  385.     return (Int_Rand_Range (least,most)) ;
  386.     } else {
  387.     return ((int)number) ;
  388.     }
  389. }
  390.  
  391. /***********************************************************************/
  392.  
  393. void Set_Ctl_C_Handler (void)
  394. /*
  395. PURPOSE: To install the function which will be called when the user
  396.      hits control-C or control-break ;
  397. */
  398. {
  399.     (void)signal(SIGINT,Ctl_C_Handler) ;
  400. }
  401.  
  402. /***********************************************************************/
  403.  
  404. void Ctl_C_Handler (void)
  405. /*
  406. PURPOSE: To get all the Ctl-Cs that arrive and if the user REALLY wants
  407.      to exit we let him say so by hitting ESC, else we carry on.
  408. */
  409. {
  410.     extern FILE* Rec_File ;
  411.  
  412.     Print_Message (CTLC_MSG) ;
  413.     (void)signal (SIGINT,SIG_IGN) ; /* Disable Ctl-C processing for now */
  414.     if (getch() == ESC_KEY) {
  415.     Go_Text_Mode () ;
  416.     if (Rec_File != NULL) {
  417.         fprintf (Rec_File,"\nUser hit Ctl-c or Ctl-break.\n") ;
  418.         fclose (Rec_File) ;
  419.     }
  420.     exit (1) ;  /* Exit because of control-c */
  421.     } else {
  422.     Clear_Help_Line () ;
  423.     (void)signal (SIGINT,Ctl_C_Handler) ;
  424.     }
  425. }
  426.  
  427. /*************************************************************************/
  428.